package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.material.IPurchaseContractProductDao;
import yl.hs.bmipfull.service.material.IPurchaseContractPriceAdjustDocService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  销售合同调价单控制层<br/>
 * 创建时间: 2021年06月03日 09时12分21秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "采购合同调价单Web接口", tags = "采购管理——采购合同调价单操作")
@RequestMapping(value = "/api/purchasecontractPriceAdjustDoc")
@RestController
public class PurchaseContractPriceAdjustDocController extends ControllerBase {
    @Autowired
    IPurchaseContractPriceAdjustDocService service;
    @Autowired
    ISearchViewService svSVC;
    @ApiOperation(value = "查询销售合同调价单", notes = "查询销售合同调价单")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "查询销售合同调价单", notes = "分页查询销售合同调价单")
    @RequestMapping(value = "view/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPageView(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPageView(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询销售合同调价单", notes = "分页查询销售合同调价单")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询销售合同调价单", notes = "根据销售合同调价单编号查询销售合同调价单")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存销售合同调价单", notes = "查询/更新销售合同调价单信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveContractPriceAdjustDoc(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除销售合同调价单信息", notes = "根据销售合同调价单编号删除系销售合同调价单信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "销售合同调价单编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据产品进行调价信息接口", notes = "根据产品进行调价信息接口")
    @RequestMapping(path = "/priceadjustbyproduction", method = RequestMethod.POST)
    public WebResult PriceAdjustByProduction(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        try {
           var result = service.PriceAdjustByProduction(submitModel.getData(), submitModel.getSn());
           if(result>0){
               return success("调整成功");
           }
        }catch (Exception ee){
               return error("添加失败");
        }
        return  error("添加失败");
    }
}